<template>
  <div class="userAdd">
    <el-form ref="userForm" :model="member" label-width="80px" :rules="rules">
      <el-form-item label="成员ID" prop="id">
        <el-input
          :disabled.sync="config.isChange"
          v-model="member.id"
        ></el-input>
      </el-form-item>
      <el-form-item label="名字" prop="name">
        <el-input v-model="member.name"></el-input>
      </el-form-item>
      <el-form-item prop="isUse">
        <el-switch
          v-model="member.isUse"
          active-text="启用"
          inactive-text="禁用"
        >
        </el-switch>
      </el-form-item>
      <el-form-item>
        <el-button @click="save" type="primary"
          ><span v-if="isChange">修改</span><span v-else>添加</span></el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { truncate } from "fs";
export default {
  name: "UserAdd",
  props: ["isChange", "fMember"],
  data() {
    return {
      member: {
        id: "",
        name: "",
        isUse: true
      },
      config: {
        isChange: false
      },
      rules: {
        id: [{ required: true, message: "请输入成员Id", trigger: "blur" }],
        name: [{ required: true, message: "请输入成员姓名", trigger: "blur" }]
      }
    };
  },
  created() {
    if (this.isChange != null) {
      this.config.isChange = this.isChange;
      this.member = JSON.parse(JSON.stringify(this.fMember));
    }
  },
  watch: {
    isChange(val) {
      this.config.isChange = this.isChange;
    },
    fMember(val) {
     this.member = JSON.parse(JSON.stringify(this.fMember));
    }
  },
  methods: {
    save() {
      //调用服务
      this.$refs["userForm"].validate(valid => {
        if (valid) {
          let that = this;
          this.$axios
            .get("member/save", {
              params: this.member
            })
            .then(res => {
              let data = res.data;
              if (data.code === 0) {
                this.$message({ message: '修改成功', type: 'success'});
                this.closeBox();
              } else {
                this.$message.error(data.msg);
              }
            })
            .catch(function(error) {
              console.log("axios请求异常:" + error);
               that.$message.error("网路请求异常");
            });
        } else {
          return false;
        }
      });
    }
    ,closeBox(){
      this.$emit('input',false);
      this.$emit('flush-data');
    }
  }
};
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="scss">
</style>
